<template>
  <div>
    <el-form :inline="true" :model="formStatue" class="demo-form-inline">
      <el-form-item label="错误级别">
        <el-select v-model="formStatue.statue" placeholder="级别">
          <el-option label="正常" value="info"></el-option>
          <el-option label="警告" value="warning"></el-option>
          <el-option label="危险" value="danger"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
      style="width: 100%"
    >
      <el-table-column prop="text" label="内容" width="180"> </el-table-column>
      <el-table-column prop="user" label="操作人" width="180">
      </el-table-column>
      <el-table-column prop="ip" label="ip地址" width="180"> </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
        <template slot-scope="scope">
          {{ scope.row.createTime.replace('T', ' ') }}
        </template>
      </el-table-column>
      <el-table-column prop="statue" label="级别" width="180">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.statue === 'info'">{{
            scope.row.statue
          }}</el-tag>
          <el-tag type="warning" v-if="scope.row.statue === 'warning'">{{
            scope.row.statue
          }}</el-tag>
          <el-tag type="danger" v-if="scope.row.statue === 'dangerous'">{{
            scope.row.statue
          }}</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <div class="block" style="margin-top: 15px">
      <el-pagination
        align="center"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.length"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getAll, getstatue } from '@/api/log';
export default {
  created() {
    getAll().then((resp) => {
      this.tableData = resp.data;
      console.log(this.tableData);
    });
  },
  methods: {},
  data() {
    return {
      tableData: [],
      currentPage: 1, // 当前页码
      total: 20, // 总条数
      pageSize: 5, // 每页的数据条数
      formStatue: {
        statue: '',
      },
    };
  },
  methods: {
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
    onSubmit() {
      console.log(this.formStatue);
      getstatue(this.formStatue).then((resp) => {
        this.tableData = resp.data;
        this.$message({
          message: '查询成功',
          type: 'success',
        });
      });
    },
  },
};
</script>
